import { instance } from '@/services/jy-api/api';
import type { ActionType, ProColumns } from '@ant-design/pro-components';
import {
    PageContainer,
    ProTable,
} from '@ant-design/pro-components';
import { Link } from '@umijs/max';
import React, { useEffect, useRef } from 'react';
import { DatePicker } from 'antd';
import moment from 'moment'
const { RangePicker } = DatePicker;
const dateFormat = 'YYYY/MM/DD';


export default (props) => {
    const { timeParams, departmentId, type } = props;
    console.log({ timeParams })
    const actionRef = useRef<ActionType>();
    const columns: ProColumns<API.RuleListItem>[] = [
        {
            title: '所属部门',
            dataIndex: 'short_name',
            hideInSearch: true,
            onCell: (item, index) => {

                let data = 0
                if (index == 0) {
                    data = item.x - item.index
                } else if (item.index == 0) {
                    data = item.x
                } else {
                    data = 0
                }

                // const data = (item.x == 1 || index == 0) ? 0 : item.index % (item.x - 1)
                const dd = index % 2 === 0 ? 2 : 0
                console.log(data)

                return {
                    rowSpan: data,
                }
            },
        },
        {
            title: '账号',
            dataIndex: 'mobile',
            hideInSearch: true,
            // onCell: (_, index) => ({
            //     colSpan: index === 1 ? 2 : 1,
            // }),
        },
        {
            title: '姓名',
            dataIndex: 'username',
            hideInSearch: true,
        },
    ];

    const request = async (params: {
        // querya
        /** 当前的页码 */
        current?: number;
        /** 页面的容量 */
        pageSize?: number;
        time: any[]
    },
        options?: { [key: string]: any },) => {
        const { time, ...data } = params
        return new Promise(async (resolve) => {
            // const res = await instance('/index/Screen/everyone', {
            const res = await instance('/index/Screen/loginPersonnel', {
                method: 'GET',
                params: {
                    ...data,
                    start_time: timeParams ? moment(timeParams[0]).format('YYYY-MM-DD') : undefined,
                    end_time: timeParams ? moment(timeParams[1]).format('YYYY-MM-DD') : undefined,
                    department_id: departmentId,
                    type: type
                },
            })
            if (!res.data) {
                resolve({
                    data: []
                })
                return
            }
            console.log(res.data.user)
            // res.data[0].user = res.data[0].user.concat(...res.data[0].user)
            // res.data[1].user = res.data[1].user.concat(...res.data[1].user)
            const list = res.data.lists.reduce((prev, current) => {
                current.user = current.user.map((item, index) => {
                    return {
                        ...item,
                        short_name: current.short_name,
                        x: current.user.length,
                        index
                    }
                })
                return prev.concat(...current.user)
            }, [])
            console.log({ list })
            resolve({
                data: list
            })
        })
        return (() => {
            console.log(1123123)
        })()
    }

    useEffect(() => {
        actionRef.current?.reload()
        console.log('fff')
    }, [departmentId])

    const getRowClassName = (record, index) => {
        let className = '';
        className = index % 2 === 0 ? "oddRow" : "evenRow";
        return className
    }


    return <div>
        <ProTable<API.RuleListItem, API.PageParams>
            actionRef={actionRef}
            rowKey="key"
            request={request}
            columns={columns}
            search={false}
            toolBarRender={false}
            rowClassName={getRowClassName}
        />
    </div>
}